package lex.title.liquid;

import java.awt.*;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.AbstractBorder;
import javax.swing.plaf.UIResource;

public class LiquidWindowBorder extends AbstractBorder implements UIResource {
	private static final LiquidWindowBorder instance = new LiquidWindowBorder();
	private boolean previousActiveState = false;
	private Window window;
	/** indicates whether the window is active */
	private boolean isActive = true;

	public static LiquidWindowBorder getInstance() {
		return instance;
	}

	public Insets getBorderInsets(Component c) {
		return new Insets(0, 4, 4, 4);
	}

	public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
		window = SwingUtilities.getWindowAncestor(c);
		if (window instanceof JDialog) {
			JDialog d = (JDialog) window;
			if (d.isModal()) {
				isActive = true;
			} else {
				isWindowRealActive(window);
			}
		} else {
			isWindowRealActive(window);
		}
		int titleHeight = UIManager.getInt("InternalFrame.frameTitleHeight");
		drawLeftTop(g, isActive, 4, titleHeight);
		// =========================================
		g.translate(0, titleHeight);
		drawLeft(g, isActive, 4, h - titleHeight - 4);
		// =========================================
		g.translate(0, -titleHeight);
		g.translate(0, h - 4);
		// getSkinBottom().draw(g, index, w, 4);
		drawBottom(g, isActive, w, 4);
		// =========================================
		g.translate(0, -(h - 4));
		g.translate(w - 4, 0);
		drawRightTop(g, isActive, 4, titleHeight);
		// =========================================
		g.translate(0, titleHeight);
		drawRight(g, isActive, 4, h - titleHeight - 4);
		// =========================================
		g.translate(0, -titleHeight);
		g.translate(-(w - 4), 0);
	}

	private void isWindowRealActive(Window window) {
		isActive = window.isActive();
		if (isActive) {
			previousActiveState = true;
		}
		if (false == previousActiveState && false == isActive) {
			isActive = true;
		}
	}

	private void drawLeftTop(Graphics g, boolean isSelected, int w, int h) {
		Color c = isSelected ? new Color(62, 145, 235) : new Color(175, 214, 255);
		g.setColor(c);
		g.fillRect(0, 0, w, h);
		c = isSelected ? new Color(94, 172, 255) : new Color(226, 240, 255);
		g.setColor(c);
		g.drawLine(0, 0, w, 0);
		g.drawLine(0, 0, 0, h);
		c = isSelected ? new Color(60, 141, 228) : new Color(170, 207, 247);
		g.setColor(c);
		g.drawLine(1, 1, w, 1);
		for (int i = 4; i < h; i += 4) {
			c = isSelected ? new Color(59, 138, 223) : new Color(166, 203, 242);
			g.setColor(c);
			g.drawLine(1, i, w, i);
			c = isSelected ? new Color(60, 141, 228) : new Color(170, 207, 247);
			g.setColor(c);
			g.drawLine(1, i + 1, w, i + 1);
		}
		c = isSelected ? new Color(47, 111, 180) : new Color(135, 164, 196);
		g.setColor(c);
		g.drawLine(w - 1, h - 1, w - 1, h - 1);
	}

	private void drawLeft(Graphics g, boolean isSelected, int w, int h) {
		Color c = isSelected ? new Color(62, 145, 235) : new Color(175, 214, 255);
		g.setColor(c);
		g.fillRect(0, 0, w, h);
		c = isSelected ? new Color(94, 172, 255) : new Color(226, 240, 255);
		g.setColor(c);
		g.drawLine(0, 0, 0, h);
		c = isSelected ? new Color(59, 138, 223) : new Color(166, 203, 242);
		g.setColor(c);
		g.drawLine(1, 0, w, 0);
		for (int i = 3; i < h; i += 4) {
			c = isSelected ? new Color(59, 138, 223) : new Color(166, 203, 242);
			g.setColor(c);
			g.drawLine(1, i, w, i);
			c = isSelected ? new Color(60, 141, 228) : new Color(170, 207, 247);
			g.setColor(c);
			g.drawLine(1, i + 1, w, i + 1);
		}
		c = isSelected ? new Color(47, 111, 180) : new Color(135, 164, 196);
		g.setColor(c);
		g.drawLine(w - 1, 0, w - 1, h);
	}

	private void drawRightTop(Graphics g, boolean isSelected, int w, int h) {
		Color c = isSelected ? new Color(62, 145, 235) : new Color(175, 214, 255);
		g.setColor(c);
		g.fillRect(0, 0, w, h);
		c = isSelected ? new Color(94, 172, 255) : new Color(226, 240, 255);
		g.setColor(c);
		g.drawLine(0, 0, w - 2, 0);
		c = isSelected ? new Color(60, 141, 228) : new Color(170, 207, 247);
		g.setColor(c);
		g.drawLine(0, 1, w - 2, 1);
		for (int i = 4; i < h; i += 4) {
			c = isSelected ? new Color(59, 138, 223) : new Color(166, 203, 242);
			g.setColor(c);
			g.drawLine(0, i, w - 2, i);
			c = isSelected ? new Color(60, 141, 228) : new Color(170, 207, 247);
			g.setColor(c);
			g.drawLine(0, i + 1, w - 2, i + 1);
		}
		c = isSelected ? new Color(94, 172, 255) : new Color(226, 240, 255);
		g.setColor(c);
		g.drawLine(0, h - 1, 0, h - 1);
		c = isSelected ? new Color(47, 111, 180) : new Color(135, 164, 196);
		g.setColor(c);
		g.drawLine(w - 1, 0, w - 1, h);
	}

	private void drawRight(Graphics g, boolean isSelected, int w, int h) {
		Color c = isSelected ? new Color(62, 145, 235) : new Color(175, 214, 255);
		g.setColor(c);
		g.fillRect(0, 0, w, h);
		c = isSelected ? new Color(94, 172, 255) : new Color(226, 240, 255);
		g.setColor(c);
		g.drawLine(0, 0, 0, h);
		c = isSelected ? new Color(59, 138, 223) : new Color(166, 203, 242);
		g.setColor(c);
		g.drawLine(1, 0, w, 0);
		for (int i = 3; i < h; i += 4) {
			c = isSelected ? new Color(59, 138, 223) : new Color(166, 203, 242);
			g.setColor(c);
			g.drawLine(1, i, w, i);
			c = isSelected ? new Color(60, 141, 228) : new Color(170, 207, 247);
			g.setColor(c);
			g.drawLine(1, i + 1, w, i + 1);
		}
		c = isSelected ? new Color(47, 111, 180) : new Color(135, 164, 196);
		g.setColor(c);
		g.drawLine(w - 1, 0, w - 1, h);
	}

	private void drawBottom(Graphics g, boolean isSelected, int w, int h) {
		Color c = isSelected ? new Color(62, 145, 235) : new Color(175, 214, 255);
		g.setColor(c);
		g.fillRect(1, 0, w - 1, h - 1);
		c = isSelected ? new Color(94, 172, 255) : new Color(226, 240, 255);
		g.setColor(c);
		g.drawLine(3, 0, w - 4, 0);
		g.drawLine(0, 0, 0, h - 2);
		c = isSelected ? new Color(47, 111, 180) : new Color(135, 164, 196);
		g.setColor(c);
		g.drawLine(0, h - 1, w, h - 1);
		g.drawLine(w - 1, 0, w - 1, h - 1);
	}
}
